{include file="common/mobile/header" title="重置密码" /}

<link rel="stylesheet" href="__CSS__/mobile-login.css?v=__V__"/>

<div class="login-container" id="resetPassword">
    <div class="login-main">
        <div class="login-header layui-clear">
            <table>
                <tr>
                    <td align="right">
                        <div class="logo">
                            <img src="{$siteConfig.slogo}"/>
                        </div>
                    </td>
                    <td align="left">
                        <div class="title">
                            <h2>{$enterprise.title}</h2>
                            <p>{$siteConfig.siteSlogan}</p>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
        <div class="login-body">
            <div class="login-types">
                {if condition="$loginType['mobile']"}
                <!-- 手机号重置 -->
                <div class="login-type" data-type="mobile" style="display: none;">
                    <div class="login-item">
                        <i class="layui-icon layui-icon-cellphone"></i>
                        {if condition="!empty($userPhone)"}
                        <input class="layui-input" type="text" name="mobile" placeholder="手机号" value="{$userPhone}"
                               readonly/>
                        {else/}
                        <input class="layui-input" type="text" name="mobile" placeholder="手机号"/>
                        {/if}
                    </div>
                    <div class="login-item">
                        <i class="layui-icon layui-icon-cellphone"></i>
                        <input class="layui-input" type="text" name="smscode" placeholder="验证码"/>
                        <a href="javascript:;" class="get-smscode-btn">获取验证码</a>
                        <span class="smscode-left-time" style="display: none;"></span>
                    </div>
                    <div class="login-item">
                        <i class="layui-icon layui-icon-password"></i>
                        <input class="layui-input" type="password" name="password" placeholder="请输入新密码"/>
                    </div>
                </div>
                {/if}
            </div>
            <div class="login-btns">
                <div class="login-btn">
                    <button class="layui-btn layui-btn-fluid login-submit">重&nbsp;置&nbsp;密&nbsp;码</button>
                </div>
            </div>
        </div>
        <div class="login-footer">
            {$siteConfig.copyright}
        </div>
    </div>
</div>

<div id="sendSmsCodeDialogTpl" style="display: none;">
    <div class="send-smscode-dialog">
        <input type="text" name="code" placeholder="请输入图形验证码" required autocomplete="off"
               class="layui-input mobile-captcha-code"/>
        <img class="captcha mobile-captcha" src="{:captcha_src()}"
             onclick="this.src='{:captcha_src()}?_='+new Date().getTime();"/>
    </div>
</div>

<script>
    //显示第一个登录内容
    var type = $("#resetPassword .login-types .login-type").first().show().attr("data-type");
    $("#resetPassword .login-types").attr("data-type", type);

    var smsCodeLeftCounterIdx = -1;

    /**
     * 发送验证码按钮
     */
    $(".get-smscode-btn").click(function () {
        var mobile = $("#resetPassword input[name='mobile']").val();
        if (!checkMobile(mobile)) {
            layer.alert("手机号格式错误");
            return;
        }
        $("#mobileCaptcha").click();
        layer.open({
            type: 1
            , title: '发送短信验证码'
            , shade: 0.8
            , id: 'getSmsCodeDialog' //设定一个id，防止重复弹出
            , btn: ['发送', '取消']
            , content: $("#sendSmsCodeDialogTpl").html()
            , yes: function (index, layero) {
                var mobileCaptchaCode = $(".layui-m-layer .mobile-captcha-code").val();
                if (!mobileCaptchaCode) {
                    layer.alert("请输入图形验证码");
                    return;
                }
                g.loading();
                $.post("/core/enterprise.sms/sendCode", {
                    mobile: mobile,
                    captchaCode: mobileCaptchaCode
                }, function (ret) {
                    g.loading(false);
                    if (ret.code == 1) {
                        layer.msg(ret.msg);
                        layer.close(index);
                        $(".smscode-left-time").text("60s").attr("left", "60").show();
                        $(".get-smscode-btn").hide();
                        smsCodeLeftCounterIdx = setInterval(function () {
                            var left = $(".smscode-left-time").attr("left");
                            if (left <= 0) {
                                clearInterval(smsCodeLeftCounterIdx);
                                $(".smscode-left-time").hide();
                                $(".get-smscode-btn").show().text("重新发送");
                            } else {
                                left = left - 1;
                                $(".smscode-left-time").attr("left", left).text(left + "s");
                            }
                        }, 1000);
                    } else {
                        $("#mobileCaptcha").click();
                        layer.alert(ret.msg);
                    }
                }, "json");
            }
        });
    });


    $("#resetPassword .login-submit").click(function () {
        var type = $("#resetPassword .login-types").attr("data-type");
        if (type == 'mobile') {
            doMobileResetPassword();
        }
    });

    /**
     * 手机号登录
     */
    function doMobileResetPassword() {
        var mobile = $("#resetPassword .login-type[data-type='mobile'] input[name='mobile']").val();
        var smscode = $("#resetPassword .login-type[data-type='mobile'] input[name='smscode']").val();
        var password = $("#resetPassword .login-type[data-type='mobile'] input[name='password']").val();

        if (!checkMobile(mobile)) {
            layer.alert("请输入正确的手机号");
            return;
        }
        if (!smscode) {
            layer.alert("请输入验证码");
            return;
        }
        if (!password) {
            layer.alert("请输入密码");
            return;
        }

        g.loading();
        $.post("/core/enterprise.user/mobileResetPassword?eid={$eid}", {
            mobile: mobile,
            smscode: smscode,
            password: password
        }, function (ret) {
            g.loading(false);
            if (ret.code == 1) {
                layer.msg(ret.msg);
                document.location.href = "/core/enterprise/login?eid={$eid}";
            } else {
                layer.alert(ret.msg);
            }
        }, "json");
    }

    /**
     * 检查手机号
     *
     * @param mobile
     * @returns {boolean}
     */
    function checkMobile(mobile) {
        if (!mobile) {
            return false;
        }
        var mobileReg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if (!mobileReg.test(mobile)) {
            return false;
        } else {
            return true;
        }
    }


    $(document).ready(function () {
        g.setElementVerticalMiddle("#resetPassword", 10);
    });
</script>

{include file="common/mobile/footer"/}
